//
//  GradeView.swift
//  FeastBooking
//
//  Created by Imon on 2017/9/9.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

class GradeView: UIView {
    
    let stars: [UIImageView] = {
        var imgViews: [UIImageView] = []
        for i in 0...4 {
            let imgView = UIImageView()
            imgView.contentMode = .scaleAspectFill
            imgView.layer.masksToBounds = true
            imgViews.append(imgView)
        }
        return imgViews
    }()
    
    let gradeLabel: UILabel = {
        let label = UILabel()
        label.textColor = gradeColor
        label.font = AppFont(size: 13)
        return label
    }()
    
    let gradeSuffix: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.font = AppBoldFont(size: 13)
        label.text = LanguageHelper.getString(key: "GRADE")
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        for i in 0...stars.count - 1 {
            self.addSubview(stars[i])
        }
        self.addSubview(gradeLabel)
        self.addSubview(gradeSuffix)
    }
    
    func initValue(grade: CGFloat) {
        gradeLabel.text = "\(grade)"
        for i in 0...stars.count - 1 {
            let temp = grade - CGFloat(i)
            if temp > 1 {
                //满星
                stars[i].image = UIImage(named: "full_star")
            } else {
                if temp >= 0.8 {
                    //满星
                    stars[i].image = UIImage(named: "full_star")
                } else if temp >= 0.3 {
                    //半星
                    stars[i].image = UIImage(named: "half_star")
                } else {
                    //空星
                    stars[i].image = UIImage(named: "empty_star")
                }
                if i + 1 <= (stars.count - 1) {
                    //剩下的全部空星
                    for j in (i + 1)...(stars.count - 1) {
                        stars[j].image = UIImage(named: "empty_star")
                    }
                }
                break
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        let starWidth: CGFloat = 15
        let gap: CGFloat = 3
        for i in 0...stars.count - 1 {
            stars[i].frame = CGRect(x: (starWidth + gap) * CGFloat(i), y: 2, width: starWidth, height: starWidth)
        }
        let textMaxSize = CGSize(width: bounds.width, height: 14)
        let gradeWidth = TextSize.textSize(text: gradeLabel.text!, font: gradeLabel.font, maxSize: textMaxSize).width
        let suffixWidth = TextSize.textSize(text: gradeSuffix.text!, font: gradeSuffix.font, maxSize: textMaxSize).width
        gradeLabel.frame = CGRect(x: (starWidth + gap) * CGFloat(stars.count) + 10, y: 3, width: gradeWidth, height: 14)
        gradeSuffix.frame = CGRect(x: gradeLabel.frame.origin.x + gradeWidth + 1, y: 3, width: suffixWidth, height: 14)
    }
}
